﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BaHuan.Entities;
using BaHuan.BusinessLayer;
using BaHuan.Common;

namespace BaHuan
{
    public partial class FrmBangMaPhieu : Form
    {
        BangMaPhieuMgt bangmaphieuMgt = new BangMaPhieuMgt();
        LoaiPhieuMgt loaiphieuMgt = new LoaiPhieuMgt();

        public FrmBangMaPhieu()
        {
            InitializeComponent();
        }

        private void FrmBangMaPhieu_Load(object sender, EventArgs e)
        {
            lbIDBangMaPhieu.Text = "";
            LoadCombobox();
        }

        private void LoadCombobox()
        {
            cboTenPhieu.DataSource = null;
            cboTenPhieu.DataSource = GetAllLoaiPhieu();
            cboTenPhieu.DisplayMember = "Ten";
            cboTenPhieu.ValueMember = "Id";
        }

        private void LoadGridview()
        {
            dataGridView1.DataSource = null;
            dataGridView1.AutoGenerateColumns = false;
            dataGridView1.DataSource = GetAllBangMaPhieuByIDLoaiPhieu();
        }

        private List<BangMaPhieu> GetAllBangMaPhieu()
        {
            ResultObj result = bangmaphieuMgt.GetAllBangMaPhieu();
            List<BangMaPhieu> lstBMP = (List<BangMaPhieu>)result.Result;
            return lstBMP;
        }

        private List<LoaiPhieu> GetAllLoaiPhieu()
        {
            ResultObj result = loaiphieuMgt.GetAllLoaiPhieu();
            List<LoaiPhieu> lstLP = (List<LoaiPhieu>)result.Result;
            return lstLP;
        }

        private List<BangMaPhieu> GetAllBangMaPhieuByIDLoaiPhieu()
        {
            ResultObj result = bangmaphieuMgt.GetAllBangMaPhieuByIDLoaiPhieu(((LoaiPhieu)cboTenPhieu.SelectedItem).Id);
            List<BangMaPhieu> lstBMP = (List<BangMaPhieu>)result.Result;
            return lstBMP;
        }

        private void cboTenPhieu_SelectedIndexChanged(object sender, EventArgs e)
        {
            LoadGridview();
            txtDienGiai.Enabled = false;
            txtMaPhieu.Enabled = false;
            txtMaPhieu.Text = "";
            txtDienGiai.Text = "";
        }

        private void btnThem_Click(object sender, EventArgs e)
        {
            txtDienGiai.Enabled = true;
            txtMaPhieu.Enabled = true;
            txtDienGiai.Text = "";
            txtMaPhieu.Text = "";
            btnLuu.Enabled = true;
            btnSua.Enabled = false;
            btnXoa.Enabled = false;

        }

        private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            txtDienGiai.Enabled = false;
            txtMaPhieu.Enabled = false;
            txtMaPhieu.Text = dataGridView1.CurrentRow.Cells[1].Value.ToString();
            txtDienGiai.Text = dataGridView1.CurrentRow.Cells[3].Value.ToString();
            lbIDBangMaPhieu.Text = dataGridView1.CurrentRow.Cells[2].Value.ToString();

            btnSua.Enabled = true;
            btnXoa.Enabled = true;
        }

        private void btnLuu_Click(object sender, EventArgs e)
        {
            if (txtMaPhieu.Text == "" || txtDienGiai.Text == "")
            {
                MessageBox.Show("Vui lòng nhập đầy đủ thông tin", "Thông báo!");
                return;
            }
            BangMaPhieu bangmaphieu = new BangMaPhieu();
            bangmaphieu.MaSo = txtMaPhieu.Text;
            bangmaphieu.DienGiai = txtDienGiai.Text;
            bangmaphieu.IdLoaiPhieu = Convert.ToInt32(cboTenPhieu.SelectedValue.ToString());
            if (lbIDBangMaPhieu.Text == "")
            {
                bangmaphieuMgt.SaveNewBangMaPhieu(bangmaphieu);
            }
            else
            {
                bangmaphieu.Id = Convert.ToInt32(lbIDBangMaPhieu.Text);
                bangmaphieuMgt.UpdateBangMaPhieu(bangmaphieu);
            }
            LoadGridview();
            MessageBox.Show("Đã Lưu","Thông báo!");

            txtMaPhieu.Enabled = false;
            txtDienGiai.Enabled = false;
            btnSua.Enabled = true;
            btnXoa.Enabled = true;
        }

        private void btnXoa_Click(object sender, EventArgs e)
        {
            if (lbIDBangMaPhieu.Text != "")
            {
                DialogResult result;
                result = MessageBox.Show("Bạn có chắc chắn muốn xóa mã phiếu này không?", "Xóa mục chi", MessageBoxButtons.YesNo);
                if (result == DialogResult.Yes)
                {
                    BangMaPhieu bangmaphieu = new BangMaPhieu();
                    bangmaphieu.Id = Convert.ToInt32(lbIDBangMaPhieu.Text);
                    bangmaphieuMgt.DeleteBangMaPhieu(bangmaphieu);
                    LoadGridview();
                }
                else
                {
                    MessageBox.Show("Mã phiếu chưa được xóa","Thông báo");
                }
            }
        }

        private void btnSua_Click(object sender, EventArgs e)
        {
            txtMaPhieu.Enabled = true;
            txtDienGiai.Enabled = true;
            btnLuu.Enabled = true;
            btnXoa.Enabled = true;
        }

        private void btnThoat_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void cboTenPhieu_KeyDown(object sender, KeyEventArgs e)
        {
            e.SuppressKeyPress = true;
        }
    }
}
